var global_cursor;

function getURLParameter(name) {
    return decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]);
};

function showMore(cursor) {
	$('#showmore').hide();
	$('#waiting').show();
	gapi.client.bapi.blogPostEndpoint.listBlogPost({'cursor': cursor}).execute(function(resp) {
		if (!resp.code) {
			if (resp != null) {
				var jsonData = resp.items;
				global_cursor = resp.nextPageToken;
				if (jsonData != null) {
					var output = "";
					for (i=0; i<jsonData.length; i++) {
						output += "<div class='article'>";
						output += "<div class='meta'>";
						output += "<p class='date'>" + jsonData[i].date + "</p>";
						output += "<h1><a href='blog.html?id=" + jsonData[i].id + "'>" + jsonData[i].title + "</a></h1>";
						output += "<p class='author'>by <a href='blog.html?author=" + jsonData[i].meta + "'>" + jsonData[i].meta + "</a></p>";
						output += "</div>";
						output += "</div>";
					}
					$('#waiting').hide();
					$('#main_thread').append(output);
					$('#showmore').show();	
				} else {
					$('#waiting').hide();
					$('#showmore').hide();
				}
			}
		}
	});
};

function init() {
	var apiRoot = 'https://tinspham.appspot.com/_ah/api';
	//var apiRoot = window.location.host + '/_ah/api';
	gapi.client.load('bapi', 'v1', function() {
		var id = getURLParameter('id');
		var author = getURLParameter('author');
		if (id == 'null') {
			gapi.client.bapi.blogPostEndpoint.listBlogPost().execute(function(resp) {
				if (!resp.code) {
					var jsonData = resp.items;
					global_cursor = resp.nextPageToken;
					var output = "";
					for (i=0; i<jsonData.length; i++) {
						output += "<div class='article'>";
						output += "<div class='meta'>";
						output += "<p class='date'>" + jsonData[i].date + "</p>";
						output += "<h1><a href='blog.html?id=" + jsonData[i].id + "'>" + jsonData[i].title + "</a></h1>";
						output += "<p class='author'>by <a href='blog.html?author=" + jsonData[i].meta + "'>" + jsonData[i].meta + "</a></p>";
						output += "</div>";
						output += "</div>";
					}
					$('#main_thread').html(output);
					$('#waiting').hide();
					$('#main_thread').show();
					$('#showmore').show();
					$('#post').show();
					$('#footer').show();
				} else {
					$('#main_thread').html('Server error. Try refresh your browser.');
					$('#waiting').hide();
					$('#main_thread').show();
				}
			});
		} else {
			gapi.client.bapi.blogPostEndpoint.getBlogPost({'id': id}).execute(function(resp) {
				if (!resp.code) {
					var jsonData = resp;
					var output = "";
					output += "<div class='article'>";
					output += "<div class='meta'>";
					output += "<p class='date'>" + jsonData.date + "</p>";
					output += "<h1><a href='blog.html?id=" + jsonData.id + "'>" + jsonData.title + "</a></h1>";
					output += "<p class='author'>by <a href='blog.html?author=" + jsonData.meta + "'>" + jsonData.meta + "</a></p>";
					output += "</div>";
					output += "<div class='text'>" + jsonData.content + "</div>";
					output += "</div>";
					$('#main_thread').html(output);
					$('#waiting').hide();
					$('#main_thread').show();
					
					$('#disqus_thread').show();
					var disqus_shortname = 'tinspham';
					var dsq = document.createElement('script');
					dsq.type = 'text/javascript';
					dsq.async = true;
					dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
					(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
					
					$('#post').show();
					$('#footer').show();
				} else {
					$('#main_thread').html('Server error. Try refresh your browser.');
					$('#waiting').hide();
					$('#main_thread').show();
				}
			});
		}
	}, apiRoot);
};